package cn.com.dhcc.adam.spring.mvc.six;

import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class MyFileUploadController {

	@RequestMapping(value = "/up", method = RequestMethod.GET)
	public String up(Model model) {
		return "six/up";
	}

	@RequestMapping(value = "/upFile",method = RequestMethod.POST)
	public String handleFormUpload(@RequestParam("name") String name,
			@RequestParam("file") MultipartFile file, Model model) {
		if (!file.isEmpty()) {
            FileOutputStream fos = null;
            try {
                byte[] bytes = file.getBytes();
                fos = new FileOutputStream("D:\\"+file.getOriginalFilename());
                fos.write(bytes);
    			model.addAttribute("upresult", "上传成功");
    			fos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
		} else {
			model.addAttribute("upresult", "上传失败");
		}

		return "six/upresult";
	}

}
